﻿package ua.mihaylenko.canvas.gui.photoshop
{
	import fl.controls.ColorPicker;
	import fl.controls.ComboBox;
	import fl.controls.NumericStepper;
	import fl.events.ColorPickerEvent;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	import ua.mihaylenko.canvas.controller.CanvasEvent;
	import ua.mihaylenko.canvas.data.DataManager;
	import ua.mihaylenko.canvas.controller.EventManager;
	import ua.mihaylenko.canvas.data.GlobalSettings;
	import ua.mihaylenko.canvas.gui.GUIMain;
	import ua.mihaylenko.canvas.gui.photoshop.canvas.GUICanvas;
	import ua.mihaylenko.canvas.gui.photoshop.scrollbar.GUIScrollBar
	import ua.mihaylenko.canvas.gui.photoshop.scrollbar.ScrollBarEvent;
	import ua.mihaylenko.canvas.gui.GUISimpleWindow;
	import ua.mihaylenko.canvas.utils.PopUpManager;
	//import ua.mihaylenko.canvas.gui.photoshop.scrollbar.ScrollBarEvent
	/**
	 * ...
	 * @author Mihaylenko A.L.
	 */
	public class GUIPhotoshop extends GUISimpleWindow
	{
		public var mcAlert:MovieClip;
		public var btnZoom:MovieClip;
		public var btnRotate:MovieClip;
		public var _smcEnabled:Sprite;
		public var _mcOrient:MovieClip;
		public var _mcEdge:MovieClip;
		public var _csbRotate:GUIScrollBar;
		public var _csbZoom:GUIScrollBar;
		public var _flColor:ColorPicker;
		public var _btnShow3D:SimpleButton;
		public var _cCanvas:GUICanvas;
		public var _cSize:ComboBox;
		public var _cFrame:ComboBox;
		public var _cMedia:ComboBox;
		public var _cCorrection:ComboBox;
		private var _pRotate:PopUpManager;
		private var _pRotate1:PopUpManager;
		private var _pZoom:PopUpManager;
		private var _pZoom1:PopUpManager;
		private var _pAlert:PopUpManager;
		public var _txtPrice:TextField;
		public var _mcMasked:Sprite;
		public var _nsPrice:NumericStepper;
		public static var PRICE_NOT_NULL:Boolean = true;
		public static var THIS:GUIPhotoshop;
		private var numSizeCost:Number;
		private var numFrameCost:Number;
		
		public function GUIPhotoshop() 
		{
			EventManager.getInstance().addEventListener(CanvasEvent.VIEW_2D, onShowMe);
			EventManager.getInstance().addEventListener(CanvasEvent.VIEW_3D, show3D);
			EventManager.getInstance().addEventListener(CanvasEvent.FILE_CHANGE, onFileChange);
			EventManager.getInstance().addEventListener(CanvasEvent.SHOW_ALERT, showAlert);
			EventManager.getInstance().addEventListener(CanvasEvent.HIDE_ALERT, hideAlert);
			_flColor.addEventListener(ColorPickerEvent.CHANGE, changeColor);
			_btnShow3D.addEventListener(MouseEvent.CLICK, clickOnShow3D);
			_csbRotate.addEventListener(ScrollBarEvent.CHANGE_SCROLL_POSITION, rotateImage);
			_csbZoom.addEventListener(ScrollBarEvent.CHANGE_SCROLL_POSITION, zoomImage);
			_nsPrice.addEventListener(Event.CHANGE, onNSChange);
			btnZoom.addEventListener(MouseEvent.CLICK, resetZoom)
			btnRotate.addEventListener(MouseEvent.CLICK, rotate90)
			btnZoom.buttonMode = true;
			btnRotate.buttonMode = true;
			_pRotate = new PopUpManager("0");
			_pRotate1 = new PopUpManager("rotate");
			_pZoom = new PopUpManager("100");
			_pZoom1 = new PopUpManager("reset zoom");
			_pAlert = new PopUpManager("Alert! Your image does not fill out the entire frame area");
			parent.addEventListener(MouseEvent.MOUSE_UP, stopDragAllThumbs);
			_pRotate.attach(_csbRotate);
			_pRotate1.attach(btnRotate);
			_pZoom.attach(_csbZoom);
			_pZoom1.attach(btnZoom);
			_pAlert.attach(mcAlert);
			_btnShow3D.useHandCursor = false;
			
			_csbZoom.maxPosition = 500;
			_csbZoom.minPosition = 10;
			_csbRotate.maxPosition = 180;
			_csbRotate.minPosition = -180;
			
			_csbRotate.scrollPosition = DataManager.getInatsnce().rotation = 0;
			_csbZoom.scrollPosition = DataManager.getInatsnce().zoom = 100;
			
			_smcEnabled.visible = true;
			_cSize.addEventListener(Event.CHANGE, changeImageSize);
			_cFrame.addEventListener(Event.CHANGE, changeFrameSize);
			_cMedia.addEventListener(Event.CHANGE, changeMedia);
			_cCorrection.addEventListener(Event.CHANGE, changeCorrection);
			for (var i:int = 0; i < DataManager.getInatsnce().front.length; i++ ) {
				_cSize.addItem( { label:DataManager.getInatsnce().front[i] });
			}
			for (i = 0; i < DataManager.getInatsnce().canvas.length; i++ ) {
				_cFrame.addItem( { label:DataManager.getInatsnce().canvas[i] } );
			}
			for (i = 0; i < DataManager.getInatsnce().media.length; i++ ) {
				_cMedia.addItem( { label:DataManager.getInatsnce().media[i] } );
			}
			for (i = 0; i < DataManager.getInatsnce().correction.length; i++ ) {
				_cCorrection.addItem( { label:DataManager.getInatsnce().correction[i] } );
			}
			_mcOrient.addEventListener(MouseEvent.CLICK, onOrientClick);
			_mcEdge.addEventListener(MouseEvent.CLICK, onEdgeClick);
			
			_btnShow3D.visible = true;
			_mcMasked.visible = true;
			mcAlert.visible = false;
			THIS = this;
		}
		
		
		public function init():void 
		{
			_cCanvas.visible = false;
			_mcMasked.visible = false;
			
			setTimeout(initDelay, 25);
		}
		private function initDelay():void
		{
			_mcOrient.gotoAndStop(2);
			_mcEdge.gotoAndStop(2);
			
			mcAlert.visible = true;
			_cCanvas.visible = true;
			
			changeCorrection();
			changeMedia();
			
			onOrientClick();
			onEdgeClick();
			
			changeFrameSize();
			changeImageSize();
			
			_cCanvas.fitBitmap();
			resetZoom();
			onNSChange();
			onEdgeClick();
			onEdgeClick();
			GUIMain.THIS.checkFinishButton();
		}
		
		private function hideAlert(e:*= null):void 
		{
			mcAlert.visible = false;
		}
		
		private function showAlert(e:*= null):void
		{
			mcAlert.visible = true;
		}
		private function rotate90(e:MouseEvent):void 
		{
			var pos:int = 90;
				 if (_csbRotate.scrollPosition == 90)   pos = 180;
			else if (_csbRotate.scrollPosition == 180)  pos = -90;
			else if (_csbRotate.scrollPosition == -180) pos = -90;
			else if (_csbRotate.scrollPosition == -90)  pos = 0;
			else _csbRotate.scrollPosition = 90;
			
			_csbRotate.scrollPosition = pos;
			_cCanvas.rotateImage(pos);
			DataManager.getInatsnce().rotation = pos;
		}
		
		private function resetZoom(e:MouseEvent = null):void 
		{
			//DataManager.getInatsnce().zoom = 100;
			//_cCanvas.zoomImage(100);
			_cCanvas.resetZoom();
		}
		
		private function fitBitmap():void
		{
			_cCanvas.fitBitmap();
		}
		
		private function onNSChange(e:Event=null):void 
		{
			var value:int = 0;
			if (e) value = e.target.value;
			PRICE_NOT_NULL = (value != 0);
			DataManager.getInatsnce().numItems = value;
			GUIMain.THIS.checkFinishButton();
			_txtPrice.text = (value*(numFrameCost+numSizeCost)+" "+DataManager.getInatsnce().sCurrency)
		}
		
		private function stopDragAllThumbs(e:MouseEvent):void 
		{
			_csbRotate.stopDragThumb1(null);
			_csbZoom.stopDragThumb1(null);
		}
		
		private function onEdgeClick(e:MouseEvent = null):void 
		{
			(_mcEdge.currentFrame == 1)?_mcEdge.nextFrame():_mcEdge.prevFrame();
			_cCanvas.setEdgeOrientation(_mcEdge.currentFrame == 2);
		}
		
		private function onOrientClick(e:MouseEvent = null):void 
		{
			(_mcOrient.currentFrame == 1)?_mcOrient.nextFrame():_mcOrient.prevFrame();
			_cCanvas.setVerticalOrientation(_mcOrient.currentFrame == 1);
		}
		
		private function changeCorrection(e:Event = null):void 
		{
			if(_cCorrection.selectedIndex==0)GUICanvas.THIS.applyNone();
			if(_cCorrection.selectedIndex==1)GUICanvas.THIS.applySepia();
			if (_cCorrection.selectedIndex == 2) GUICanvas.THIS.applyMonochrome();
			if (_cCorrection.selectedIndex == 3) GUICanvas.THIS.applyBrighter();
			if (_cCorrection.selectedIndex == 4) GUICanvas.THIS.applyDarker();
			if (_cCorrection.selectedIndex == 5) GUICanvas.THIS.applyContrast();
			if (_cCorrection.selectedIndex == 6) GUICanvas.THIS.applyThreshold();
			DataManager.getInatsnce().filter = _cCorrection.selectedIndex;
		}
		
		private function changeMedia(e:Event=null):void 
		{
			
		}
		
		private function changeImageSize(event:Event = null) {
			numSizeCost = DataManager.getInatsnce().arrImage[_cSize.selectedIndex].price;
			_cCanvas.resizeImage(DataManager.getInatsnce().arrImage[_cSize.selectedIndex].w, DataManager.getInatsnce().arrImage[_cSize.selectedIndex].h);
			DataManager.getInatsnce().boxWidth = DataManager.getInatsnce().arrImage[_cSize.selectedIndex].w
			DataManager.getInatsnce().boxHeight = DataManager.getInatsnce().arrImage[_cSize.selectedIndex].h
			_txtPrice.text = (_nsPrice.value*(numFrameCost+numSizeCost)+" "+DataManager.getInatsnce().sCurrency)
		}
		private function changeFrameSize(event:Event = null) {
			numFrameCost = DataManager.getInatsnce().arrImage[_cFrame.selectedIndex].price;
			_cCanvas.resizeCanvas(DataManager.getInatsnce().arrFrame[_cFrame.selectedIndex].w);
			DataManager.getInatsnce().boxDepth = DataManager.getInatsnce().arrFrame[_cFrame.selectedIndex].w;
			_txtPrice.text = (_nsPrice.value*(numFrameCost+numSizeCost)+" "+DataManager.getInatsnce().sCurrency)
		}
		private function onFileChange(event:CanvasEvent) {
			_smcEnabled.visible = false;
			_mcMasked.visible = false;
		}
		private function rotateImage(event:ScrollBarEvent) {
			_pRotate.text = event.position.toString();
			if (_cCanvas)_cCanvas.rotateImage(event.position);
			DataManager.getInatsnce().rotation = event.position;
		}
		private function zoomImage(e:ScrollBarEvent):void 
		{
			_pZoom.text = e.position.toString();
			if (_cCanvas)_cCanvas.zoomImage(e.position);
		}
		private function onShowMe(event:CanvasEvent) {
			//this.visible = true;
			_cCanvas.visible = true;
			_mcMasked.visible = false;
		}
		private function clickOnShow3D(event:MouseEvent) {
			EventManager.getInstance().dispatchEvent(new CanvasEvent(CanvasEvent.VIEW_3D));
		}
		private function show3D(event:CanvasEvent) {
			//this.visible = false;
			_cCanvas.visible = false;
			_mcMasked.visible = true;
		}
		private function changeColor(event:ColorPickerEvent) {
			_cCanvas.bgColor = _flColor.selectedColor;
			DataManager.getInatsnce().color = _flColor.selectedColor;
		}
		
	}
	
}